<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/5/24
 * Time: 17:14
 */

namespace App\Base;

use App\Entity\User;

use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\SerializerInterface;

class BaseController4App extends BaseController {


    public $data = [];
    public $request;
    public $baseCache;
    public $is ;

    public function __construct(
        BaseCache $baseCache
        ,LoggerInterface $logger
        ,SerializerInterface $serializer
    )
    {
        parent::__construct();

        $this->serializer = $serializer;
        $this->logger = $logger;
        $this->baseCache = $baseCache;
        $request = Request::createFromGlobals();
        $this->request = $request;
//        $token = $request->headers->get("token");
//        $nowtime = $request->headers->get("nowtime");
//        if (md5($nowtime.BaseConfig::__VI__) != $token) {
//            echo time();
//            die("I KNOW U");
//        }
    }

    /**
     * @return mixed|User
     */
    public function getUser()
    {
        $rep = $this->getDoctrine()->getRepository(User::class);
        $request = Request::createFromGlobals();
        $tk = $request->headers->get("X-AUTH-TOKEN");
        $user = $rep->findOneBy(array("ApiToken"=>$tk));
        if ($user) {
            return $user;
        }else{
            return false;
        }
    }


    public function needToBeUser()
    {
        if (!$user = $this->getUser()) {
            header('content-type:application/json');
            header('charset:utf8');
            header("HTTP/1.1 203 Non-Authoritative Information");
            $s = 203;
            $i = "正在进行身份验证";
            $re = array(
                "s"    => $s,
                "i"    => $i,
                "d"    => [],
            );
            die(json_encode($re));
        }
        return $user;
    }

    public function json_out($s,$i,$d = array())
    {
        $context = [];
        if ($this->groups) {
            $context["groups"] = $this->groups;
        }

        $data = ["s"=> $s,"i"=> $i,"d"=>$d];
        if ($this->meta) {
            $data["meta"] = $this->meta;
        }
        return $this->json($data,200,["content-type"=>"application/json","access-token"=>$this->baseCache->getUserAccessToken($this->request)],$context);
    }

    public function setError($str,$url = "",$delay = 0)
    {
        $this->data["error"] = $str;
        $this->data["redirect"] = $url;
        $this->data["delay"] = $delay;
    }

    public function userCache()
    {
        $access_token = $this->baseCache->getUserAccessToken($this->request);
        return $this->baseCache->getItem($access_token);
    }

    public function setUserCache($key,$val)
    {
        $uc = $this->userCache();
        if (!is_array($uc)) {
            $uc = [];
        }
        $uc[$key] = $val;
        $access_token = $this->baseCache->getUserAccessToken($this->request);
        $this->baseCache->setItem($access_token,$uc);
    }

    public function getUserCache($key)
    {
        $uc = $this->userCache();
        if (!is_array($uc)) {
            return null;
        }
        return $uc[$key] ?? null;
    }
}
